<?php
namespace app\admin\controller;
use app\admin\model\KfCate;

class Kf extends Common{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $input = input('post.');
        $input = array_merge(['cate_id'=>0,'keyword'=>""],$input);
        $KfModel = new \app\admin\model\Kf();
        $Kf_list = $KfModel->all($input);
        $this->assign([
            'kf_list' => $Kf_list['data'],
            'kf_page' => $Kf_list['page'],
            'cat_list' => GetTree($this->getKfCateAll()),
            'input'    => $input
        ]);
        return $this->fetch();
    }
    public function add()
    {
        $this->assign([
            'cat_list' => GetTree($this->getKfCateAll()),
            'default_nickname' => randString(6)
        ]);
        return $this->fetch();
    }
    //添加一个客服
    public function insert()
    {
        /**
         * 参数说明
         * kf_username 客服用户名
         * kf_password 客服密码
         * kf_nickname 客服昵称
         * kf_default_nickname 客服默认昵称
         */
        $post = ArrRemoveJs(input('post.'));
        //匿名初始化
        $post['nickname'] = trim($post['nickname']) == "" ? ($post['default_nickname'] != "" ? $post['default_nickname'] : randString(6)) : $post['nickname'];

        $kf = new \app\admin\model\Kf();
        $result = $kf->addKf($post);

        $this->success($result['msg'],url('index'));
    }

    //ajax删除客服
    public function del()
    {
        /**
         * 参数说明
         * param.id
         */
        $input  = !empty(input('param.id')) ? [input('param.id')] : null;
        if(empty($input))
        {
            return ['error'=>1,'msg'=>'没有客服被选中'];
        }
        $KfModel = new \app\admin\model\Kf();
        return $KfModel->del($input);
    }

    //删除多个客服
    public function delAll()
    {
        /**
         * 参数说明
         * post.id[]
         */
        $input = input('post.');
        if(!isset($input['id']) || empty($input['id']))
        {
            $this->error("没有选中客服");
        }
        $KfModel = new \app\admin\model\Kf();
        $result = $KfModel->del($input['id']);
        $this->success($result['msg'],url('index'));
    }


    //编辑一个客服
    public function edit()
    {
        /**
         * 参数说明
         * param.id
         */
        $id = input('param.id');
        $KfModel = new \app\admin\model\Kf();
        $data = $KfModel->info(['kf_id'=>$id]);
        if(!$data) $this->error('不存在该客服');
        $this->assign([
            'cat_list' => GetTree($this->getKfCateAll()),
            'id' =>$id,
            'info'=>$data
        ]);
        return $this->fetch();
    }

    public function update()
    {
        /**
         * 参数说明
         * kf_username 客服用户名
         * kf_password 客服密码
         * kf_nickname 客服昵称
         */
        $input = ArrRemoveJs(input('post.'));
        $KfModel = new \app\admin\model\Kf();
        $result = $KfModel->edit($input);
        if($result['error'] != 0)
        {
            $this->error($result['msg']);
        }
        else
        {
            $this->success($result['msg'],url('index'));
        }
    }

    public function getKfCateAll()
    {
        $KfCate = new KfCate();
        return $KfCate->all();
    }

    public function setAdmin($id)
    {
        /*
         * 参数说明
         * param .id
         * return error,msg
         */
        $KfModel = new \app\admin\model\Kf();
        return $KfModel->setAdmin($id);
    }

    public function cancleAdmin($id)
    {
        /*
         * 参数说明
         * param .id
         * return error,msg
         */
        $KfModel = new \app\admin\model\Kf();
        return $KfModel->cancleAdmin($id);
    }
}